package com.mtt.erp.freight.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.mtt.erp.common.domain.MttErpConstant;
import com.mtt.erp.common.domain.QueryRequest;
import com.mtt.erp.common.exception.MttErpException;
import com.mtt.erp.common.utils.MttErpUtil;
import com.mtt.erp.common.utils.SortUtil;
import com.mtt.erp.common.utils.UUIDUtils;
import com.mtt.erp.freight.domain.McbFile;
import com.mtt.erp.freight.dao.McbFileMapper;
import com.mtt.erp.freight.dto.McbCreateDto;
import com.mtt.erp.freight.dto.McbDto;
import com.mtt.erp.freight.dto.McbUpdateDto;
import com.mtt.erp.freight.service.IMcbFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mtt.erp.freight.vo.McbFileDetailVo;
import com.mtt.erp.freight.vo.McbFileVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author MrBird
 */
@Service
public class McbFileServiceImpl extends ServiceImpl<McbFileMapper, McbFile> implements IMcbFileService {

    @Value("${erp.max.batch.insert.num}")
    private int batchInsertMaxNum;

    @Resource
    McbFileMapper mcbFileMapper;

    @Override
    @Transactional
    public void createMcb(McbCreateDto mcbCreateDto) {
        //添加商品
        McbFile mcbFile = McbFile.builder().build();

        BeanUtils.copyProperties(mcbCreateDto, mcbFile);
        /* mcbFile.setMcb00(freightId());   //生成运费模板Id*/
        mcbFile.setMcb00(UUIDUtils.getUUID());
        mcbFile.setMcbcdate(LocalDateTime.now());
        //商品是属于哪一个用户的
        Long currentUserId = MttErpUtil.getCurrentUser().getUserId();
        mcbFile.setMcbcuser(currentUserId.toString());
        this.save(mcbFile);
    }

    @Override
    @Transactional
    public void updateMcb(McbUpdateDto mcbUpdateDto) {
        McbFile mcbFile = McbFile.builder().build();
        BeanUtils.copyProperties(mcbUpdateDto, mcbFile);
        Long currentUserId = MttErpUtil.getCurrentUser().getUserId();
        mcbFile.setMcbmdate(LocalDateTime.now());
        mcbFile.setMcbmuser(currentUserId.toString());
        this.updateById(mcbFile);
    }

    @Override
    @Transactional
    public void deleteMcb(String[] mcbIds) throws MttErpException {
        this.removeByIds(Arrays.asList(mcbIds));

    }

    @Override
    public IPage<McbFileDetailVo> findMcb(QueryRequest request,McbDto mcbDto) {
        mcbDto.setMcbcuser(String.valueOf(MttErpUtil.getCurrentUser().getUserId()));
        Page<McbFileDetailVo> page = new Page<>(request.getPageNum(), request.getPageSize());//查询使用
        SortUtil.handlePageSort(request, page, true);
        IPage<McbFileDetailVo> mcbDetail = this.mcbFileMapper.getMcbDetail(page,mcbDto);
        return mcbDetail;
    }

    /**
     * 运费模板详情（包含快递信息）
     *
     * @param id
     * @return
     */
    @Override
    public McbFileDetailVo findCategoryById(String id) {
        return this.mcbFileMapper.getMcaDetailbyId(id);
    }

}
